package com.tg.app.activity.device.settings;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsListBaseActivity;
import com.appbase.custom.base.SettingData;

import java.util.ArrayList;

public class AreaAlarmPlanWeekActivity extends DeviceSettingsListBaseActivity {
    private ArrayList<SettingData> list = new ArrayList<>();
    private  char[]  selects  = {0,0,0,0,0,0,0};
    public  final  static String EXT_WEEK_SELECTS ="ext_week_selects";
    @Override
    protected void onSetupFailed() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        char[] s =  getIntent().getCharArrayExtra(EXT_WEEK_SELECTS);
        if (s!=null){
            selects = s;
        }
        initView();
        initData();
    }

    @Override
    protected void initView() {
        setTitle(R.string.area_alarm_mode_plan_settings);

        getAdapter().setIconImageResource(R.mipmap.week_day_check);

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SettingData  data = list.get(position);
                if (data.getFlag() == 0) {
                    data.setFlag(1);
                    selects[6-position] = '1';
                } else {
                    data.setFlag(0);
                    selects[6-position] = '0';
                }
                getAdapter().notifyDataSetChanged();

            }
        });

        View footerView = LayoutInflater.from(this).inflate(R.layout.layout_list_btn, null);
        getListView().addFooterView(footerView, null, false);
        footerView.findViewById(R.id.btn_list_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(EXT_WEEK_SELECTS,selects);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

    }



    private void initData() {
        String[] weeks = getResources().getStringArray(R.array.weeks);
        for (int i=0;i<weeks.length;i++){
            SettingData data = new SettingData(weeks[i], 0);
            if (selects[6-i] =='1'){
                data.setFlag(1);
            }else {
                data.setFlag(0);
            }

            list.add(data);
        }
        setDataList(list);
        getAdapter().notifyDataSetChanged();
    }
}
